
layui.define(['jquery', 'form', 'layer', 'element','table'], function(exports) {
    var $ = layui.jquery,
        form = layui.form,
        layer = layui.layer,
        element = layui.element,
        table = layui.table;
    //var jsonarray='';
    var start='';
    var emptystr = "[]";
    var emptyjsonarray = eval('(' + emptystr + ')');
    // var jsonstr='[ { "id": 0, "month_name": "19年1月", "startday": "2019-01-01", "endday": "2019-01-31", "status": "0" }, { "id": 1, "month_name": "19年2月", "startday": "2019-02-01", "endday": "2019-02-28", "status": "0" }, { "id": 2, "month_name": "19年3月", "startday": "2019-03-01", "endday": "2019-03-31", "status": "0" }, { "id": 3, "month_name": "19年4月", "startday": "2019-04-01", "endday": "2019-04-30", "status": "0" }, { "id": 4, "month_name": "19年5月", "startday": "2019-05-01", "endday": "2019-05-31", "status": "0" }, { "id": 5, "month_name": "19年6月", "startday": "2019-06-01", "endday": "2019-06-30", "status": "1" } ]';
    // var jsonarray = eval('('+jsonstr+')');
    var jsonstr = "[]";
    var jsonarray = eval('(' + jsonstr + ')');
    var isbox = '';
    renderdruginfoevent();
    form.render('select', 'sel_srf');
    
    function renderdruginfo(jsonarray){
    //TODO 渲染药品信息表格
        table.render({
            elem: '#DrugList',
            limits: [3,5,10]
            ,limit: 10 ,//每页默认显示的数量
            width:$(".ui_content").width(),
            cols: [
                [ {
                    field: 'drugId',title: '药品ID',sort: true,width:'10%'
                }, {
                    field: 'drugName',title: '药品名称'
                },{
                    field: 'drugSpecifications',title: '药品规格'
                },{
                    field: 'drugUnit',title: '药品单位'
                },{
                    field: 'priceIn',title: '入库价'
                },{
                    field: 'priceOut',title: '出库价'
                },{
                    field: 'status',title: '状态',templet: function (d) {
                        if (d.status == "1") {  // 0 为启用 1 为停用
                            return '<span class="layui-btn layui-btn-online layui-btn-xs">正常</span>';
                        } else {
                            return '<span class="layui-btn layui-btn-disabled layui-btn-xs">停用</span>';
                        }
                    }
                },
                    {
                    field: 'edit',title: '操作',toolbar: '#edittype',unresize: true,width:'10%'
                }]
            ],
            data:jsonarray,
            event: true,
            page: true,
            loading: true //请求数据时，是否显示loading
        });
    }



    function renderdruginfoevent(){
    //TODO 渲染药品信息表格事件
        table.on('tool(useruv)', function(obj){
            var data = obj.data;
            if(obj.event === 'edit'){
                $('#drug_id').val(data.drugId);
                $('#drug_name').val(data.drugName);
                $('#drug_specifications').val(data.drugSpecifications);
                $('#drug_unit').val(data.drugUnit);
                $('#zdy').val(data.zdy);
                $('#price_in').val(data.priceIn);
                $('#price_out').val(data.priceOut);
                start = 1;
                $('#AdddrugForm').modal('show');
                form.render();
            }else if(obj.event === 'stop'){
                drug_stop(data.drugId,'DrugList');
            }
        });
    }

    Search();
    
    
    function Search(){
    	 //TODO 查询
        // CheckSession();
        $.ajax(
            {
                type: "get",
                async: false,
                url: "/drug-info/listByStatus",
                data:{
                    // org_id:GetSessionKeyVale('org_id'),
                    status:'1'
                },
                success:function (data) {
                    if(data.code == 200){
                        jsonarray = data.data;
                        renderdruginfo(jsonarray);
                    }else{
                        layer.msg(data.msg, {
                            icon: 2,
                            time: 1000
                        });
                    }
                }
            }
        );
    }

    
    $("#btnSearch").click(function() {
        //TODO 查询
    	Search();
    });

    $('#adddrug').click(function(event) {
        //TODO 增加药品
        $('#AdddrugForm').modal('show');
        start=0;
        $('.layui-input').val('');
        $('#drug_name').focus();
        form.render();
    });

    $('#btnAdddrugConfirm').click(function(event) {
        // TODO 保存事件
        $('.layui-form-danger').removeClass('layui-form-danger');
        if(start == 0){
            if(CheckComplete()==true){
                adddrugrow();
            }else{
                return false;
            }
        }else if (start == 1){
        	if(CheckComplete()==true){
                editpaytyperow();
        	}else{
                return false;
        	}
        }
    });

    function CheckComplete(){
        //TODO 检查信息填写完整性
        var sRet='';
        var aRet='';
        var bRet=true;
        if($('#drug_name').val()==''||$('#drug_name').val()==undefined){
            sRet='药品名称';
            aRet='drug_name';
        }else if ( $('#drug_specifications').val()==''||$('#drug_specifications').val()==undefined ){
            sRet='药品规格';
            aRet='drug_specifications';
        }else if ( $('#drug_unit').val()==''||$('#drug_unit').val()==undefined ){
            sRet='药品单位';
            aRet='drug_unit';
        }else if ( $('#price_in').val()==''||$('#price_in').val()==undefined || $('#price_in').val()==0 ){
            sRet='入库价';
            aRet='price_in';
        }else if ( $('#price_out').val()==''||$('#price_out').val()==undefined || $('#price_out').val()==0 ){
            sRet='出库价';
            aRet='price_out';
        }
        if(sRet!=''){
            layer.msg(sRet+'信息有误！请检查', {
                icon: 5,
                time: 1000
            });
            $('#'+aRet).focus();
            $('#'+aRet).addClass('layui-form-danger');
            bRet=false;
        }

        return bRet;
    }



    function drug_stop(id,tableid) {
    //TODO 切换项目状态
    //     CheckSession();
        layer.confirm('确认要停用/启用吗？', function(index) {
            for(var i=0; i<jsonarray.length; i++)
            {
                if(jsonarray[i].drugId==id){
                    $.ajax(
                        {
                            type: "PUT",
                            async: false,
                            url: "/drug-info/stop",
                            data: {
                                drugId:id
                            },
                            success:function (data) {
                            	if(data.code==200){
                                    if (data.data.status == -1) {
                                        jsonarray.splice(i, 1);
	                                    layer.msg('已停用!', {
	                                        icon: 5,
	                                        time: 1000
	                                    });
                                    } else if (data.data.status == 1) {
                                        jsonarray[i].status = '1';
	                                    layer.msg('已启用!', {
	                                        icon: 6,
	                                        time: 1000
	                                    });
	                                }
	                                table.reload(tableid,{
	                                    data : jsonarray
	                                });
                            	}else{
                            		layer.msg(data, {
                                        icon: 5,
                                        time: 1000
                                    });
                            	}     
                            }
                        }
                    );
                }
            }
        });
    }


    function editpaytyperow(){
        //TODO 修改药品信息
        // CheckSession();
        $.ajax({
            type: "PUT",
            async: false,
            url: "/drug-info/update",
            data: {
                drugId:$('#drug_id').val(),
                drugName:$('#drug_name').val(),
                zdy:$('#zdy').val(),
                drugSpecifications:$('#drug_specifications').val(),
                drugUnit:$('#drug_unit').val(),
                priceIn:$('#price_in').val(),
                priceOut:$('#price_out').val(),
                lastOperator:'admin'
            },
            success:function (data) {
                if(data.code==200){
                    for(var i=0; i<jsonarray.length; i++)
                    {
                        if(jsonarray[i].drugId==data.data.drugId){
                            jsonarray[i].drugName=data.data.drugName;
                            jsonarray[i].zdy=data.data.zdy;
                            jsonarray[i].py=data.data.py;
                            jsonarray[i].wb=data.data.wb;
                            jsonarray[i].drugSpecifications=data.data.drugSpecifications;
                            jsonarray[i].drugUnit=data.data.drugUnit;
                            jsonarray[i].priceIn=data.data.priceIn;
                            jsonarray[i].priceOut=data.data.priceOut;
                            jsonarray[i].lastOperator=data.data.lastOperator;
                        }
                    }
                    table.reload('DrugList',{
                        data : jsonarray
                    });
                    $('#AdddrugForm').modal('hide');
                    layer.msg('修改成功!', {
                        icon: 1,
                        time: 1000
                    });
                    renderdruginfoevent();  //渲染操作事件
                } else{
                    layer.msg(data.msg, {
                        icon: 2,
                        time: 1000
                    });
                }
            }
        });
    }


    function adddrugrow(){
    	//TODO 新增药品
        // CheckSession();
        $.ajax(
            {
                type: "post",
                async: false,
                url: "/drug-info/insert",
                data: {
                    drugName:$('#drug_name').val(),
                    drugSpecifications:$('#drug_specifications').val(),
                    drugUnit:$('#drug_unit').val(),
                    priceIn:$('#price_in').val(),
                    priceOut:$('#price_out').val(),
                    zdy:$('#zdy').val()
                },
                success:function (data) {
                	//console.log(data)
                	if(data.code==200){
                      jsonarray.push(data.data);     //从后插入
                      table.reload('DrugList',{
                          data : jsonarray
                      });
                        layer.msg('新增成功!', {
                            icon: 1,
                            time: 1000
                        });
                	}else{
                        layer.msg(data.msg, {
                            icon: 2,
                            time: 1000
                        });
                    }
                    $('#AdddrugForm').modal('hide');
                    renderdruginfoevent();  //渲染操作事件

                }
            }
        );

    }



    $("#condition").bind("input propertychange", function (e) {
        //TODO 药品信息查询事件
        renderdruginfo(emptyjsonarray);
        renderdruginfoevent();
        if ($('#condition').val() == "") {
            renderdruginfo(jsonarray);
        } else {
            renderdruginfo(emptyjsonarray);
            var cookjsonstr = "[]";
            var cookjsonarray = eval('(' + cookjsonstr + ')');
            var srf = $('#srf').val();
            switch(srf) {
                case 'wb':
                    for (var i = 0; i < jsonarray.length; i++) {
                        if (jsonarray[i].wb.indexOf($("#condition").val()) >= 0 ) {
                            var arr = jsonarray[i];
                            cookjsonarray.push(arr);
                            table.reload('DrugList', {
                                data: cookjsonarray
                            });
                            renderdruginfoevent();
                        }
                    }
                    break;
                case 'zdy':
                    for (var i = 0; i < jsonarray.length; i++) {
                        if (jsonarray[i].zdy.indexOf($("#condition").val()) >= 0 ) {
                            var arr = jsonarray[i];
                            cookjsonarray.push(arr);
                            table.reload('DrugList', {
                                data: cookjsonarray
                            });
                            renderdruginfoevent();
                        }
                    }
                    break;
                case 'py':
                    for (var i = 0; i < jsonarray.length; i++) {
                        if (jsonarray[i].py.indexOf($("#condition").val()) >= 0 ) {
                            var arr = jsonarray[i];
                            cookjsonarray.push(arr);
                            table.reload('DrugList', {
                                data: cookjsonarray
                            });
                            renderdruginfoevent();
                        }
                    }
                    break;
            }

        }
    });

});


